//
//  HHRefreshViewController.m
//  HHBroker
//
//  Created by Herui on 9/5/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHRefreshViewController.h"

@interface HHRefreshViewController () <MJRefreshBaseViewDelegate>

@end

@implementation HHRefreshViewController
{
}

- (void)dealloc
{
    if (_headerView) {
        [_headerView free];
    }
    if (_footerView) {
        [_footerView free];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self customiseRefreshView];
}


- (void)selectedRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView) {
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        
    }
    
}

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

- (void)customiseRefreshView
{
    MJRefreshHeaderView *headerView = [MJRefreshHeaderView header];
    self.headerView = headerView;
    
    MJRefreshFooterView *footerView = [MJRefreshFooterView footer];
    self.footerView = footerView;
    
    self.headerView.scrollView = self.tableView;
    self.headerView.delegate = self;
    self.footerView.scrollView = self.tableView;
    self.footerView.delegate = self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.headerView.hidden = YES;
    // Do any additional setup after loading the view.
    
    [self loadTableViewDataSourceWithState:HHRefreshStateNormal];
}

- (void)scrollToTop
{
    if (self.tableView) {
        if (self.dataArray.count > 0) {
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }
    }
}

#pragma mark - 请求数据
// 子类重写
- (void)loadTableViewDataSourceWithState:(HHRefreshState)state
{
    
}


#pragma mark - MJRefreshBaseViewDelegate
// 开始进入刷新状态就会调用
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    self.isRefreshing = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (refreshView == self.headerView) {
            // 重新向服务器请求数据
            self.page = 0;
            [self.dataArray removeAllObjects];
            [self loadTableViewDataSourceWithState:HHRefreshStatePullDown];
            
            
        }
        if (refreshView == self.footerView) {
            // 重新向服务器请求数据
            self.page++;
            [self loadTableViewDataSourceWithState:HHRefreshStatePulUp];
        }
    });
}
// 刷新完毕就会调用
- (void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView
{
    self.isRefreshing = NO;
}
// 刷新状态变更就会调用
- (void)refreshView:(MJRefreshBaseView *)refreshView stateChange:(MJRefreshState)state;
{
    
}



- (void)setIsNotRefreshData:(BOOL)isNotRefreshData{
    if (isNotRefreshData) {
        self.footerView.hidden = YES;
    }else{
        self.footerView.hidden = NO;
    }
}

#pragma mark - 结束刷新调用的方法
- (void)endRefreshing
{
    if (self.headerView) {
        [self.headerView endRefreshing];
    }
    if (self.footerView) {
        [self.footerView endRefreshing];
    }
}

#pragma mark - 解析数据 子类实现
- (void)getJSONWithResponse:(id)response tag:(NSInteger)tag
{
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (self.headerView.hidden && _notFirstAppear) {
        self.headerView.hidden = NO;
    }
    _notFirstAppear = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.headerView.hidden == NO && !self.isRefreshing) {
        self.headerView.hidden = YES;
    }
}

@end

